﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace BookDownload
{
    public partial class mainForm : Form
    {
        //是否正在下载
        public bool isLoading;
        //下载线程
        Thread threadLoad;
        //测试时读取页面线程
        Thread threadPage;
        BookLoad bookload;
        BookLoad bookpage;
        ChapterList chapterList;
        public mainForm()
        {
            InitializeComponent();
            isLoading = false;
        }

        private void btStart_Click(object sender, EventArgs e)
        {
           
            if (isLoading == true)
            {
                if (threadLoad != null && threadLoad.IsAlive == true)
                {
                    threadLoad.Abort();
                    Thread.Sleep(500);
                }
                isLoading = false;
                statusText.Text = "就绪";
                btStart.Text= "开始";

            }
            else
            {
                isLoading = true;
                btStart.Text = "停止";
                statusText.Text = "获取书籍列表...";
                tbMessage.Text = "";
                loadBook();
            }  
        }
        /// <summary>
        /// 下载书籍
        /// </summary>
        private void loadBook()
        {

            bookload = new BookLoad(this,tbURL.Text,Convert.ToInt32(tbStart.Text));
            threadLoad = new Thread(new ThreadStart(bookload.load));
            threadLoad.IsBackground = true;
            threadLoad.Start();
            btStart.Text = "停止";
        }
        /// <summary>
        /// 下载测试页面的HTML内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btTestReadPage_Click(object sender, EventArgs e)
        {
                       
            if (threadPage != null && threadPage.IsAlive == true)
            {
                threadLoad.Abort();
                Thread.Sleep(500);
            }
            bookpage = new BookLoad(this, tbTestURL.Text, 1,tbTestCharset.Text);
            threadPage = new Thread(new ThreadStart(bookpage.getTestPage));
            threadPage.IsBackground = true;
            threadPage.Start();
            statusText.Text = "读取页面中...";
        }
        /// <summary>
        /// 测试书名正则表达式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btTestBookReg_Click(object sender, EventArgs e)
        {
            string html = tbTestResult.Text;
            if(html==""){
                 MessageBox.Show("请先读取网页！");
                 return;
            }
            string reg = tbTestBook.Text;
            string book = GetData.getTitle(html, reg);
            if(book!="")
                MessageBox.Show("成功获得书名:" + book);
            else
            {
                MessageBox.Show("获得书名为空，请检查！");
            }
        }
        /// <summary>
        /// 测试章节正则表达式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btTestChapterReg_Click(object sender, EventArgs e)
        {
            string html = tbTestResult.Text;
            if (html == "")
            {
                MessageBox.Show("请先读取网页！");
                return;
            }
            string reg = tbTestChapter.Text;
            chapterList= GetData.getList(html, reg);
            if (chapterList!=null)
                MessageBox.Show("成功获得章节数：" + chapterList.Title.Count);
            else
            {
                MessageBox.Show("章节信息获取失败！");
            }
        }

        private void btTestGetContentHtml_Click(object sender, EventArgs e)
        {
            if (chapterList == null)
            {
                MessageBox.Show("请先成功获取章节信息！");
                return;
            }
            if (threadPage != null && threadPage.IsAlive == true)
            {
                threadLoad.Abort();
                Thread.Sleep(500);
            }
            //读取第一个网页
            string realChapterURL = GetData.getRealChapterURL(tbTestURL.Text, chapterList.URL[0].ToString());
            bookpage = new BookLoad(this,realChapterURL, 1,tbTestCharset.Text);
            threadPage = new Thread(new ThreadStart(bookpage.getTestPage));
            threadPage.IsBackground = true;
            threadPage.Start();
            statusText.Text = "读取页面中...";
        }

        private void btTestContentReg_Click(object sender, EventArgs e)
        {
            string html = tbTestResult.Text;
            if (html == "")
            {
                MessageBox.Show("请先读取网页！");
                return;
            }
            string reg = tbTestContent.Text;
            string exceptstring=tbTestExcept.Text;
            string result = GetData.getContent(html, reg, exceptstring);

            if (result != "")
                MessageBox.Show(result);
            else
            {
                MessageBox.Show("内容获取失败！");
            }
        }

        private void btTestSave_Click(object sender, EventArgs e)
        {
            string host = GetData.getOnlyHostName(tbTestURL.Text);
            string path = @"config/" + host + ".json";
            StreamWriter wt = new StreamWriter(path, false);
            string result = tbTestCharset.Text;
            result += Environment.NewLine + tbTestBook.Text;
            result += Environment.NewLine + tbTestChapter.Text;
            result += Environment.NewLine + tbTestContent.Text;
            result += Environment.NewLine + tbTestExcept.Text;
            wt.WriteLine(result);
            wt.Close();
            MessageBox.Show("已经保存为"+path);
        }

        private void timer_Tick(object sender, EventArgs e)
        {

        }

    }
}
